package com.scalar.chiptrack.operations.tapereel.actions;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.operations.tapereel.forms.TapeReelRequestForm;
import com.scalar.chiptrack.operations.tapereel.TapeReelRequest;
import com.scalar.chiptrack.operations.tapereel.dao.TapeReelRequestDAO;
import com.scalar.chiptrack.operations.tapereel.actioncommands.TapeReelRequestCommandFactory;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.commons.Constants;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Apr 14, 2007
 * Time: 3:29:11 PM
 */
public class TapeReelRequestAction extends ChipTrackAction implements SessionKeys, Constants
{

    private static final String ACTION_NAME = "/tapeReelRequest.do" ;

    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        HashMap errors = new HashMap();
        HashMap info    = new HashMap();
        request.setAttribute(ERROR_MESSAGES, errors);
        ActionForward actionForward = mapping.findForward("search");
        String action = request.getParameter(Constants.REQUEST_PARAM_ACTION );
        boolean addRequestAttribs = true;

        TapeReelRequestForm tapeReelRequestForm = (TapeReelRequestForm)form;
        try
        {
            //System.out.println("--------------------------------- Tape and Reel  -- action = " + action );
            if( ( action == null ) || ( action.trim().length() ==0 ) )
            {
                Logger.debug( "No Action specified. Forwarding to the search page" );
                TapeReelRequest tapeReelRequest = new TapeReelRequest();

                tapeReelRequestForm.setTapeReelRequest( tapeReelRequest );
                addRequestAttribs = true;
            }
            else
            {
                try
                {
                    ActionCommand tapeReelRerquestCommand = TapeReelRequestCommandFactory.getActionCommand( action );

                    if( tapeReelRerquestCommand == null )
                    {
                        Logger.debug( "Could not retreive the corresponding action command" );
                        errors.put( "Invalid request", null );
                    }
                    else
                    {
                        actionForward = tapeReelRerquestCommand.execute( mapping, form, request, response );
                        addRequestAttribs = false;
                    }
                }
                catch( Exception e )
                {
                    Logger.error( "Error while executing the action command", e );
                }
            }
        }
        catch( Exception e )
        {
            Logger.error( "Error while processing the request", e );
            errors.put( "Could not load the Tape and Reel Request Information" , null );
            e.printStackTrace();
        }

        if( addRequestAttribs )
        {
            request.setAttribute("partNumbersList", TapeReelRequestDAO.loadTapeAndReelFgProducts());
            request.setAttribute("vendorsList", TapeReelRequestDAO.getTrLocations());
            tapeReelRequestForm.getTapeReelRequest().setReqStatus("Issued_Pulled");
            request.setAttribute("statusList", TapeReelRequest.getStatusList());
            request.setAttribute(ERROR_MESSAGES, errors);
            request.setAttribute(INFO_MESSAGES, info);
        }
        return actionForward;
    }


}
